package xyz.thetbw.autocode

import xyz.thetbw.autocode.utils.SqlUtils
import com.pkest.table.printer.TablePrinter
import com.pkest.table.printer.TableSetting
import com.pkest.table.printer.TableTextAlign
import mu.KotlinLogging
import org.apache.velocity.VelocityContext
import xyz.thetbw.autocode.utils.VelocityUtils
import java.io.InputStreamReader
import java.lang.StringBuilder

private val logger = KotlinLogging.logger { }

fun main() {
    showBanner()
    logger.info { "开始获取数据库信息" }
    val dataModel = SqlUtils.getDataModel()
    tablePrettyPrint(dataModel)

    logger.info { "开始扫描模板" }
    val templateDescription = TemplateDescription(dataModel)
    templateDescription.load(Config.instance.loadDir)
    logger.info { "模板加载完毕" }
    println("----------------------模板信息----------------------")
    templatePrint(templateDescription.children, 0)
    println("\n--------------------------------------------------\n")



    logger.info { "开始渲染模板" }
    templateDescription.render()
    logger.info { "finished" }
}

//打印数据库表格
fun tablePrettyPrint(dataModel: DataModel) {
    println("数据库信息：${dataModel.table}\t (${dataModel.tableComment})")
    val tableRows: ArrayList<Map<String, Any?>> = ArrayList()
    dataModel.dataFields.forEach {
        val tableMap = mapOf<String, Any?>(
            "name" to it.name,
            "type" to it.type.javaTypeStr,
            "nullable" to it.nullable,
            "length" to (it.length ?: ""),
            "comment" to it.comment
        )
        tableRows.add(tableMap)
    }
    val tableSetting = TableSetting()
    tableSetting.isShowNo = false
    tableSetting.textAlign = TableTextAlign.LEFT
    println(TablePrinter.DEFAULT.render(tableRows, tableSetting))
}

//打印扫描到的模板信息
fun templatePrint(templates: List<TemplateDescription.TemplateChild>, indent: Int) {
    val tableSize = 4

    for ((index,ele) in templates.withIndex()){
        val stringBuilder = StringBuilder()
        if (indent == 0) {
            stringBuilder.append("├─")
        }else{
            stringBuilder.append("│")
        }
        stringBuilder.append(" ".repeat(tableSize*indent))
        if (indent != 0){
            if (index == templates.size -1 || ele.children.isNotEmpty()){
                stringBuilder.append("└─ ")
            }else{
                stringBuilder.append("├─")
            }
        }else{
            stringBuilder.append(" ")
        }
        stringBuilder.append(ele.renderName)
        println(stringBuilder)

        if (ele.children.isNotEmpty()) {
            templatePrint(ele.children, indent+1,)
        }
    }
}

/**
 * 显示版本信息
 */
fun showBanner(){
    val versionInfo = Version.load();
    val bannerInput = Version::class.java.classLoader.getResourceAsStream("banner.txt")
    val bannerReader = InputStreamReader(bannerInput)
    val velocityContext = VelocityContext()
    velocityContext.put("version",versionInfo)

    bannerReader.use {
        val banner = VelocityUtils.render(bannerReader,velocityContext)
        println(banner)
    }
}
